package TransactionImplementation

import (
	"PaySalary/DataBase"
	"PaySalary/Model"
	"PaySalary/TransactionApplication"
	"time"
)

type PaydayTransaction struct {
	TransactionApplication.Transaction
	itsPayDate time.Time
	itsPaychecks map[int]Model.Paycheck
}

func (p *PaydayTransaction)NewPaydayTransaction(payDate time.Time)  {
	p.itsPayDate = payDate
}

func (p *PaydayTransaction) Execute() {
	employeeIds := DataBase.PayrollDatabase.GetAllEmployeeIds(p)

	for id := range employeeIds{
		e := DataBase.PayrollDatabase.GetEmployee(p, id)

		if e != nil {
			if e.IsPayDate(p.itsPayDate) {
				pc := Model.NewPaycheck(e.GetPayPeriodStartDate(p.itsPayDate), p.itsPayDate)
				p.itsPaychecks[id] = pc
				e.Payday(pc)
			}
		}
	}
}

func (p *PaydayTransaction) GetPaycheck(empId int) Model.Paycheck {
	if pc, ok := p.itsPaychecks[empId]; ok {
		return pc
	}
	
	return Model.Paycheck{}
}

func (p *PaydayTransaction) GetPaycheckCount() int {
	return len(p.itsPaychecks)
}